﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace ChinaDynastyTours.Helpers
{
    public class ExcelHelper
    {
        public static DataTable ExcelImport(string filePath, string sheetName)
        {
            try
            {
                //Excel就好比一个数据源一般使用
                string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 12.0;";

                OleDbConnection con = new OleDbConnection(strConn);
                con.Open();
                OleDbCommand cmd = con.CreateCommand();
                cmd.CommandText = string.Format(" select * from [{0}$]", sheetName);//[sheetName$]要如此格式
                DataTable dt = new DataTable();

                using (OleDbDataReader odr = cmd.ExecuteReader())
                {
                    dt.Load(odr);
                }
                return dt;
            }
            catch (Exception)
            {
                return null;
            }

        }

        public static bool IsInstallAccessDataBaseEngine(out string accessDataEngineDownloadUrl)
        {
            bool result = true;
            accessDataEngineDownloadUrl = "";

            Microsoft.Win32.RegistryKey subRegKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\Microsoft.ACE.ACERCLR.12.0\CLSID", false);
            if(subRegKey == null)
            {
                result = false;
                accessDataEngineDownloadUrl = "http://www.microsoft.com/zh-cn/download/details.aspx?id=23734";//AccessDataEngine2007 驱动下载地址
            }
            return result;
        }

    }
}
